//
//  BaseViewController.m
//  TouMingChuFangApp
//
//  Created by yuntai on 2017/10/26.
//  Copyright © 2017年 yuntai. All rights reserved.
//

#import "BaseViewController.h"

#ifndef TangShanTongJiAPPFull

#import "IWxChatMgrLogin.h"
#endif
@interface BaseViewController ()<CLLocationManagerDelegate>

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;
    self.currentPage = 1;
     self.view.backgroundColor = [UIColor whiteColor];
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navi-background"] forBarMetrics:UIBarMetricsDefault];
    
    self.navigationController.navigationBar.barTintColor = kNavigationColor;
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBarStyle:(UIBarStyleBlack)];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}];
    [self setBackBtn];
    
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}
//设置状态栏背景颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - public methods

-(void)setBackBtn {
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"  " style:UIBarButtonItemStyleDone target:nil action:nil];
    UINavigationBar *navigationBar = [UINavigationBar appearanceWhenContainedIn:NSClassFromString(self.description), nil];
    navigationBar.backIndicatorImage = [UIImage imageNamed:@"navi-backbtn"];
    navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"navi-backbtn"];
    
    if (self.tabBarController == nil) {
        self.navigationItem.backBarButtonItem = backItem;
    } else {
        
        if (self.tabBarController.navigationController == nil) {
            self.navigationItem.backBarButtonItem = backItem;
        }else{
            self.tabBarController.navigationItem.backBarButtonItem = backItem;
        }
    }
    
    
}

-(void)setBackBtnEventHandler:(void (^)(id sender))handler {
    UIImage *image      = [UIImage imageNamed:@"navi-backbtn"];
    UIImage *image_h    = [UIImage imageNamed:@"navi-backbtn"];
    UIButton* btn       = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.exclusiveTouch  = YES;
    NSString* backTitle = @" ";
    CGSize titleSize    = [backTitle sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17.0]}];
    btn.frame = CGRectMake(0, 0, image.size.width + ceilf(titleSize.width) + 10, MAX(image.size.height,ceilf(titleSize.height)));
    btn.titleLabel.font = [UIFont systemFontOfSize:17.0];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:image_h forState:UIControlStateHighlighted];
    [btn setImage:image_h forState:UIControlStateSelected];
    [btn setTitle:backTitle forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor clearColor];
    btn.titleEdgeInsets = UIEdgeInsetsMake(0., -15., 0., 0.);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0., -25., 0., 0.);
    
    [btn bk_addEventHandler:^(id sender) {
        handler(btn);
    } forControlEvents:UIControlEventTouchUpInside];
    
    if (self.tabBarController == nil) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    } else {
        
        if (self.tabBarController.navigationController == nil) {
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }else{
            self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }
    }
}

-(void)setLeftBtnImage:(UIImage *)btnImage eventHandler:(void (^)(id sender))handler {
    UIButton* btn       = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.exclusiveTouch  = YES;
    btn.frame           = CGRectMake(0, 0, 44, 44);
    btn.backgroundColor = [UIColor clearColor];
    [btn setImage:btnImage forState:UIControlStateNormal];
    
    [btn bk_addEventHandler:^(id sender) {
        handler(btn);
    } forControlEvents:UIControlEventTouchUpInside];
    
    if (self.tabBarController == nil) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    } else {
        
        if (self.tabBarController.navigationController == nil) {
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }else{
            self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
            
        }
    }
}

- (void)setLeftBtnImage:(UIImage *)btnImage Size:(CGSize)size eventHandler:(void (^)(id sender))handler {
    UIButton* btn       = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.exclusiveTouch  = YES;
    btn.frame           = CGRectMake(0, 0, size.width, size.height);
    btn.backgroundColor = [UIColor clearColor];
    [btn setImage:btnImage forState:UIControlStateNormal];
    
    [btn bk_addEventHandler:^(id sender) {
        handler(btn);
    } forControlEvents:UIControlEventTouchUpInside];
    
    if (self.tabBarController == nil) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    } else {
        
        if (self.tabBarController.navigationController == nil) {
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }else{
            self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
            
        }
    }
}
-(void)setLeftBtn:(NSString *)btnTitle eventHandler:(void (^)(id sender))handler
{
    UIButton* btn       = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.exclusiveTouch  = YES;
    btn.frame           = CGRectMake(0, 0, 44, 44);
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    btn.backgroundColor = [UIColor clearColor];
    [btn setTitle:btnTitle forState:UIControlStateNormal];
    
    [btn bk_addEventHandler:^(id sender) {
        handler(btn);
    } forControlEvents:UIControlEventTouchUpInside];
    
    if (self.tabBarController == nil) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    } else {
        if (self.tabBarController.navigationController == nil) {
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }else{
            self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }
    }
}

-(void)setLeftBtn:(NSString *)btnTitle imageName:(NSString *)imagename eventHandler:(void (^)(id sender))handler
{
    UIView *rightview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 65, 44)];
    rightview.tag = 100;
    
    UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(55, 19.5, 7, 5)];
    iv.tag = 102;
    iv.image = [UIImage imageNamed:imagename];
    
    UIButton* btn       = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = 101;
    btn.exclusiveTouch  = YES;
//    btn.frame           = CGRectMake(0, 0, 55, 44);
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    btn.backgroundColor = [UIColor clearColor];
    [btn setTitle:btnTitle forState:UIControlStateNormal];
    
    [btn bk_addEventHandler:^(id sender) {
        handler(btn);
    } forControlEvents:UIControlEventTouchUpInside];
    [iv sizeToFit];
    [btn sizeToFit];
    
    rightview.frame = CGRectMake(0, 0, btn.width+iv.width+6, 44);
    iv.centerY = rightview.centerY;
    iv.x = btn.width+4;
    btn.x = 2;
    btn.centerY = rightview.centerY;
    
    [rightview addSubview:btn];
    [rightview addSubview:iv];
    
    if (self.tabBarController == nil) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightview];
    } else {
        if (self.tabBarController.navigationController == nil) {
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightview];
        }else{
            self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightview];
        }
    }
}
- (void)updateLeftItemWith:(NSString *)title{
    
    UIView *rightview = [self.navigationItem.leftBarButtonItem.customView viewWithTag:100];
    UIButton *btn = [rightview viewWithTag:101];
    UIImageView *iv = [rightview viewWithTag:102];
    
    if (btn.titleLabel.text.length == title.length) {
        [btn setTitle:title forState:UIControlStateNormal];
        return;
    }
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    [iv sizeToFit];
    [btn sizeToFit];
    
    rightview.frame = CGRectMake(0, 0, btn.width+iv.width+6, 44);
    iv.centerY = rightview.centerY;
    iv.x = btn.width+4;
    btn.x = 2;
    btn.centerY = rightview.centerY;
}


-(void)setRightBtn:(NSString *)btnTitle eventHandler:(void (^)(id sender))handler
{
    UIButton* btn       = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.exclusiveTouch  = YES;
    btn.frame           = CGRectMake(0, 0, 40, 44);
    btn.backgroundColor = [UIColor clearColor];
    [btn setTitle:btnTitle forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn bk_addEventHandler:^(id sender) {
        handler(btn);
    } forControlEvents:UIControlEventTouchUpInside];
    
    if (self.tabBarController == nil) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    } else {
        if (self.tabBarController.navigationController == nil) {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }else{
            self.tabBarController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }
    }
}


-(void)setRightBtnImage:(UIImage *)btnImage eventHandler:(void (^)(id sender))handler {
    UIButton* btn       = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.exclusiveTouch  = YES;
    btn.frame           = CGRectMake(0, 0, 30, 44);
    btn.backgroundColor = [UIColor clearColor];
    [btn setImage:btnImage forState:UIControlStateNormal];
    
    [btn bk_addEventHandler:^(id sender) {
        handler(btn);
    } forControlEvents:UIControlEventTouchUpInside];
    
    if (self.tabBarController == nil) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    } else {
        if (self.tabBarController.navigationController == nil) {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }else{
            self.tabBarController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }
    }
}

-(void)setRightBtnImage:(UIImage *)btnImage Size:(CGSize)size eventHandler:(void (^)(id sender))handler {
    UIButton* btn       = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.exclusiveTouch  = YES;
    btn.frame           = CGRectMake(0, 0, size.width, size.height);
    btn.backgroundColor = [UIColor clearColor];
    [btn setImage:btnImage forState:UIControlStateNormal];
    
    [btn bk_addEventHandler:^(id sender) {
        handler(btn);
    } forControlEvents:UIControlEventTouchUpInside];
    if (self.tabBarController == nil) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    } else {
        if (self.tabBarController.navigationController == nil) {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }else{
            self.tabBarController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        }
    }
}

- (void)setCenterTitle:(NSString *)title {
    
    UILabel* lable      = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 21)];
    lable.textColor     = [UIColor whiteColor];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text          = title;
    
    if (self.tabBarController == nil) {
        [self.navigationItem.titleView addSubview:lable];
    } else {
        if (self.tabBarController.navigationController == nil) {
            
            [self.navigationItem.titleView addSubview:lable];
        }else{
            
            self.tabBarController.navigationItem.titleView = lable;
        }
    }
}

- (void)setCenterSearchViewWithDelegete:(id)delegate{
    UITextField *text = [[UITextField alloc]init];
    text.frame = CGRectMake(0, 0, kScreenBoundWidth-100, 30);
    text.placeholder = @"请输入搜索内容";
    text.text = @"请输入您感兴趣的内容,门店";
    text.textColor = [UIColor whiteColor];
    UIImageView *view = [[UIImageView alloc] init];
    view.image = [UIImage imageNamed:@"fangdajing"];
    view.frame = CGRectMake(0, 0, 35, 35);
    view.contentMode = UIViewContentModeCenter;
    
    text.leftView = view;
    text.leftViewMode = UITextFieldViewModeAlways;
    text.layer.cornerRadius = 5.f;
    text.layer.masksToBounds = YES;
    text.delegate = delegate;
    text.font = [UIFont systemFontOfSize:12];
    text.returnKeyType = UIReturnKeySearch;
    [text setBackground:[UIImage imageNamed:@"searchbgview"]];
    
    [text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.navigationItem.titleView = text;
    
}
-(void)textFieldDidChange :(UITextField *)theTextField{
    
}
- (void)setNaviBackBtn{
    UINavigationBar *navigationBar = [UINavigationBar appearanceWhenContainedIn:NSClassFromString(self.description), nil];
    navigationBar.backIndicatorImage = [UIImage imageNamed:@"navi-backbtn"];
    navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"navi-backbtn"];
    
}
-(void)setCenterBtnImage:(UIImage *)btnImage Size:(CGSize)size eventHandler:(void (^)(id sender))handler {
    UIButton* btn       = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.exclusiveTouch  = YES;
    btn.frame           = CGRectMake(0, 0, size.width, size.height);
    btn.backgroundColor = [UIColor clearColor];
    [btn setImage:btnImage forState:UIControlStateNormal];
    
    [btn bk_addEventHandler:^(id sender) {
        handler(btn);
    } forControlEvents:UIControlEventTouchUpInside];
    
    if (self.tabBarController == nil) {
        [self.navigationItem.titleView addSubview:btn];
    } else {
        if (self.tabBarController.navigationController == nil) {
            
            [self.navigationItem.titleView addSubview:btn];
        }else{
            
            self.tabBarController.navigationItem.titleView = btn;
        }
    }
}

- (void)deleteLeftBarItem {
    
    if (self.tabBarController == nil) {
        self.navigationItem.leftBarButtonItem = nil;
    } else {
        if (self.tabBarController.navigationController == nil) {
            
            self.navigationItem.leftBarButtonItem = nil;
        }else{
            
            self.tabBarController.navigationItem.leftBarButtonItem = nil;
        }
    }
}

- (void)deleteRightBarItem {
    
    if (self.tabBarController == nil) {
        self.navigationItem.rightBarButtonItem = nil;
    } else {
        if (self.tabBarController.navigationController == nil) {
            
            self.navigationItem.rightBarButtonItem = nil;
        }else{
            
            self.tabBarController.navigationItem.rightBarButtonItem = nil;
        }
    }
}

- (void)deleteCenterView {
    
    if (self.tabBarController == nil) {
        self.navigationItem.titleView = nil;
    } else {
        if (self.tabBarController.navigationController == nil) {
            
            self.navigationItem.titleView = nil;
        }else{
            
            self.tabBarController.navigationItem.titleView = nil;
        }
    }
}


#pragma mark - 定位
//开始定位
-(void)startLocation{
    
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    // 设置定位精确度到米
    
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 设置过滤器为无
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    
    if ([[[UIDevice currentDevice]systemVersion]doubleValue] >8.0){
        [self.locationManager requestWhenInUseAuthorization];
    }
//    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//        _locationManager.allowsBackgroundLocationUpdates =YES;
//    }
    [self.locationManager startUpdatingLocation];
    
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    switch (status) {
            
        case kCLAuthorizationStatusNotDetermined:
            if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization];
            }break;
        default:break;
    }
}



#pragma mark -xmpp登陆

#if TangShanTongJiAPPFull==1

-(void)loginXmppServer{
    
        if ([[IWxChatMgrLogin sharedInstance]isLoginSuccess] == NO  && self.isLoginIMing == NO) {
            [self judgeConnectandLogin];
        }else{
            WXLog(@"已经登录！！！");
        }
    
    
}
- (void)judgeConnectandLogin
{
    
    
    NSString *retState = [[IWxChatMgrLogin sharedInstance] registDelegate:self appId:@"12" secret:@"22"]; //设置代理
    if ([retState isEqualToString:@"OK"]){
        WXLog(@"%@---%@",kUserLoginName,kUserLoginPassword);
        [[IWxChatMgrLogin sharedInstance] loginWithUserId:kUserLoginName password:[kUserLoginPassword sha256]];
        self.isLoginIMing = YES;
    }
}

- (void)loginWithUserIdRspState:(EWxLoginRegState)state loginInfo:(NSDictionary *)loginInfo
{
    if (login_OK == state){
        self.isLoginIMing = NO;
    }else{
        BOOL isLogin = [kUserDefault boolForKey:kUserIsLogin];
        if (isLogin && self.isLoginIMing) {
            //如果处于登录状态但是im没有成功 就继续重新登录 --直到登录成功为止
            [[IWxChatMgrLogin sharedInstance] loginWithUserId:kUserLoginName password:kUserLoginPassword];
            
        }
        
        //重新登录
        //        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示"
        //                                                        message:@"聊天登录失败!"
        //                                                       delegate:self
        //                                              cancelButtonTitle:@"确定"
        //                                              otherButtonTitles:nil];
        //        alter.tag = 901;
        //        [alter show];
    }
}
#endif

@end
